<?php
class resetpwdService extends commonService{
	public  $resetpwd_url;
	private $key;
	
	private $receiver_id;
	private $receiver_email;
	private $receiver_name;
	
	public function __construct(){
		parent::__construct();
		$this->resetpwd_url=url('user','doit');
		$this->key='2348960dfkssdfkldf';
		$this->table_front_admin=table('front_admin');
	}
	//登陆后无法重置
	public function check_islogin(){
		if(isset($_SESSION['MY2'])){
			return true;
		}
		return false;
	}
	public function mysendmail($email,$title,$msg){
		$send_mail=new send_mail($this->conf->send_mail);
		return $send_mail->send($email,$title,$msg);
	}
	
	//检测邮箱和验证码
	public function check_ok($email,$code){
		if(!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/',$email)){
			return -1;//邮箱格式错
		}
		if($_SESSION['code']!=$code){
			return -2;//验证码错
		}
		return 1;
	}
	//检查邮件是否存在
	public function check_email_ok($email){
		$query="select `id`,`user_name` from `{$this->table_front_admin}` where `email`='{$email}' limit 1;";
		$r=$this->m->select($query);
		if(count($r)>0){
			$this->receiver_id=$r['id'];
			$this->receiver_email=$email;
			$this->receiver_name=htmlspecialchars($r['user_name'],ENT_QUOTES);
			return true;
		}
		return false;
	}
	//生成密钥
	private function create_key(){
		return substr(md5($this->receiver_id.$this->key),4,10);
	}
	//读取邮件标题和正文模板
	//$arr=array('{[username]}'=>'','{[email-link]}'=>'')
	private function load_tpl($arr){
		$filename=VIEWER_DIR.'user/resetpwd.tpl.html';
		if(!file_exists($filename)){
			exit('resetpwd.tpl.html不存在！');
		}
		$content=file_get_contents($filename);
		foreach($arr as $k=>$v){
			$content=str_replace($k,$v,$content);
		}
		return $content;
	}
	//组装部件，开始发送邮件
	public function go(){
		
		$subject='中国检科院测试评价中心用户密码重置信';
		$arr=array(
			'{[username]}'=>$this->receiver_name,
			'{[email-link]}'=>$this->resetpwd_url.'?auth='.$this->create_key().'&uid='.$this->receiver_id
		);
		$body=$this->load_tpl($arr);

		$r=$this->mysendmail($this->receiver_email,$subject,$body);
		if($r){
			//更新数据库的login_time
			$time=time();
			$query="update `{$this->table_front_admin}` set `login_time`='{$time}' where `id`='{$this->receiver_id}' limit 1";
			$this->m->query($query);
			return true;
		}else{
			return false;
		}
	}
	//判断时间是否过期
	public function check_time($id){
		$id=intval($id);
		if($id==0){
			return -1;//用户不存在
		}
		$query="select `login_time` from `{$this->table_front_admin}` where `id`='{$id}' limit 1";
		$r=$this->m->get_field($query);
		if($r==''){
			return -1;//用户不存在
		}
		if(time()>$r+3600*24*2){
			return -2;//过期
		}
		return 1;
	}
	//检测密钥与id
	public function check_auth($auth,$id){
		$this->receiver_id=intval($id);
		if(strlen($auth)!=10){
			return false;
		}
		if($this->receiver_id==0){
			return false;
		}
		if($auth!=$this->create_key()){
			return false;
		}
		return true;
	}
	//检测密码合法性
	public function check_pwd($password,$repassword){
	
		if(mb_strlen($password)<6 or mb_strlen($repassword)<6){
			return -1;//密码长度最小为6位
		}
		if($password!=$repassword){
			return -2;//密码不一致
		}
		return 1;
	}
	//更新密码
	public function update_pwd($password){
		$password=md5password($password);
		$query="update `{$this->table_front_admin}` set `user_password`='{$password}' where `id`='{$this->receiver_id}' limit 1";
		return $this->m->query($query);
	}
}